from datetime import datetime
from pydantic import BaseModel, Field, field_validator
from app.models.user import JobSeekerStatus, UserRole
import re

class TokenResponse(BaseModel):
    access_token: str
    refresh_token: str
    token_type: str

class LoginRequest(BaseModel):
    phone: str | None = Field(None, description="手机号")
    password: str = Field(description="密码")
    email: str | None = Field(None, description="邮箱")

    @field_validator('phone')
    def validate_phone(cls, v):
        if v and not re.match(r'^1[3-9]\d{9}$', v):
            raise ValueError('手机号格式不正确')
        return v

class UserBeas(BaseModel):
    phone: str = Field(max_length=11, description="手机号")
    username: str = Field(max_length=12, description="用户名")
    gender: str | None = Field(max_length=2, description="性别")
    is_active: bool = Field(default=True, description="用户状态")
    avatar: str | None = Field(None, max_length=255, description="头像图片存储地址")
    role: UserRole = Field(default=UserRole.JOB_SEEKER, description="用户角色")
    job_seeker_status: JobSeekerStatus = Field(default=JobSeekerStatus.UNEMPLOYED_SEEKING, description="求职者状态(1:离职正在找工作 2:在职急需工作 3:在职找工作 4:在职没有找工作的打算)")

    @field_validator('phone')
    def validate_phone(cls, v):
        if not re.match(r'^1[3-9]\d{9}$', v):
            raise ValueError('手机号格式不正确')
        return v

class UserCreate(UserBeas):
    email: str = Field(max_length=50, description="邮箱")
    real_name: str = Field(max_length=50, description="真实名称")
    password: str = Field(max_length=100, description="密码")

    @field_validator('password')
    def validate_password(cls, v):
        if len(v) < 6:
            raise ValueError('密码长度至少为6位')
        return v

class UserInDB(UserCreate):
    id : int
    created_at: datetime = Field(description="创建时间")
    updated_at: datetime = Field(description="更新时间")

class UserOccupied(BaseModel):
    id: int = Field(description="用户ID")
    username: str = Field(max_length=12, description="用户名")
    role: UserRole = Field(description="用户角色")


class UserUpdate(BaseModel):
    phone: str = Field(max_length=11, description="手机号")
    email: str = Field(max_length=50, description="邮箱")
    username: str = Field(max_length=12, description="用户名")
    gender: str | None = Field(max_length=2, description="性别")
    real_name: str = Field(max_length=50, description="真实名称")
    password: str = Field(max_length=100, description="密码")
    @field_validator('phone')
    def validate_phone(cls, v):
        if not re.match(r'^1[3-9]\d{9}$', v):
            raise ValueError('手机号格式不正确')
        return v

    @field_validator('email')
    def validate_email(cls, v):
        if v and not re.match(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', v):
            raise ValueError('邮箱格式不正确')
        return v

    @field_validator('password')
    def validate_password(cls, v):
        if len(v) < 8:
            raise ValueError('密码长度至少为8位')
        return v
    
class JobBase(BaseModel):
    job_name: str = Field(max_length=255, description="工作名称")
    company: str = Field(max_length=255, description="公司名称")
    company_size: int = Field(description="企业规模（员工数）")
    salary: float = Field(description="薪资", ge=0)
    detail: str = Field(description="工作详情", max_length=65535)  # 使用最大长度匹配TextField
    head: str = Field(max_length=50, description="负责人")
    posts: str = Field(max_length=50, description="负责人职位")
    company_address: str = Field(description="企业地址")
    company_industry: str = Field(max_length=100, description="企业所属行业")
    company_email: str = Field(max_length=100, description="企业邮箱")
    avatar: str | None = Field(max_length=255, description="头像图片存储地址", default=None)

    @field_validator('company_email')
    def validate_company_email(cls, v):
        if not re.match(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', v):
            raise ValueError('企业邮箱格式不正确')
        return v
    
class JobUpdate(JobBase):
    creator_id: int = Field(description="创建人")


class JobInDB(JobUpdate):
    job_id: int = Field(description="工作ID")
    created_at: datetime = Field(description="创建时间")
    updated_at: datetime = Field(description="更新时间")


class ExpectationBase(BaseModel):
    job_name: str = Field(max_length=255, description="期望职位名称")
    money: float = Field(description="期望薪资", ge=0)
    place: str = Field(max_length=255, description="期望工作地点")
    industry: str = Field(max_length=255, description="期望行业")

class ExpectationUpdate(ExpectationBase):
    pass

class ExpectationInDB(ExpectationUpdate):
    id: int = Field(description="期望ID")



class ResumeBase(BaseModel):
    resume_url: str = Field(max_length=255, description="简历存储地址")
    created_at: datetime = Field(description="创建时间")
    updated_at: datetime = Field(description="更新时间")

class ResumeUpdate(ResumeBase):
    pass

class ResumeInDB(ResumeUpdate):
    id: int = Field(description="简历ID")
        

